package controller;

import service.Service;
import extthread.ThreadA;
import extthread.ThreadB;

/**
　* @Description: TODO
　* @param ${tags}
　* @return ${return_type}
　* @throws
　* @author Merlin
　* @date 2018/6/19 19:13
　*/

/**
 * 出现异常，锁自动释放
 * 当一个线程执行的代码出现异常时，其所持有的锁会自动释放。
 */
public class Test {

	public static void main(String[] args) {
		try {
			Service service = new Service();

			ThreadA a = new ThreadA(service);
			a.setName("a");
			a.start();

			Thread.sleep(500);

			ThreadB b = new ThreadB(service);
			b.setName("b");
			b.start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出结果：
	 * ThreadName=a run beginTime=1529578316839
	   ThreadName=a run   exceptionTime=1529578317081
	   Exception in thread "a" java.lang.NumberFormatException: For input string: "a"
	   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	   at java.lang.Integer.parseInt(Integer.java:580)
	   at java.lang.Integer.parseInt(Integer.java:615)
	   at service.Service.testMethod(Service.java:15)
	   at extthread.ThreadA.run(ThreadA.java:16)
	   Thread B run Time=1529578317341
	 *
	 * 结论: 线程a出现异常并释放锁，线程b进入方法正常打印
	 * 说明出现异常的锁被自动释放了。
	 */

}
